﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;
using System.Web.Security;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class ReceiptController : Controller
    {

        [HttpPost]
        public JsonResult GetReceiptList(int jStartIndex = 0, int jPageSize = 0, string jSorting = null,
            int skippingID = 0, int employerID = 0)
        {
            try
            {
                Library_Receipts lipReceipt = new Library_Receipts();
                List<ReceiptModel> receiptList = lipReceipt.GetDataList(jStartIndex, jPageSize, jSorting, skippingID, employerID);
                return Json(new { Result = "OK", Records = receiptList, TotalRecordCount = receiptList.Count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddReceipt(ReceiptModel model)
        {
            try
            {
                Library_Receipts lipReceipt = new Library_Receipts();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                //
                int employeeID = lipReceipt.GetEmployeeIdByUsername(currentUser.UserName);
                model.EmployerID = employeeID;
                //
                var add = lipReceipt.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateReceipt(ReceiptModel model)
        {
            try
            {
                Library_Receipts lipReceipt = new Library_Receipts();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                lipReceipt.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteReceipt(int Id)
        {
            try
            {
                Library_Receipts lipReceipt = new Library_Receipts();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                lipReceipt.DeleteData(Id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }
    }
}
